package com.jingdianjichi.oss.service;

import com.jingdianjichi.oss.entity.FileInfo;
import com.jingdianjichi.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * ClassName:MinioStorageServiceImpl
 * Package:com.jingdianjichi.oss.service
 * Description:
 *
 * @Author fst
 * @Create 2024/10/3 21:25
 * @Version 1.0
 */

public class MinioStorageAdapter implements StorageAdapter
{
    @Resource
    private MinioUtil minioUtil;

    @Value("${minio.url}")
    private String url;

    @Override
    @SneakyThrows
    public void createBucket(String bucket) {
        minioUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile, String bucket, String objectName) {
        if(objectName!=null)
        minioUtil.uploadFile(multipartFile.getInputStream(),bucket,objectName+"/"+multipartFile.getOriginalFilename());
        else minioUtil.uploadFile(multipartFile.getInputStream(),bucket,multipartFile.getOriginalFilename());
    }

    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
       return minioUtil.getAllBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return minioUtil.downLoad(bucket,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteObject(String bucket, String objectName) {
         minioUtil.deleteObject(bucket,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtil.deleteBucket(bucket);
    }

    @Override
    public String getUrl(String bucketName, String objectName)
    {
        return url+"/"+bucketName+"/"+objectName;
    }
}
